package security.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import security.model.Organization;
import security.model.SystemSettings;

@Stateless
public class SystemSettingsDAO {

    @PersistenceContext(unitName = "persistence")
    private EntityManager em;

    public SystemSettings getById(Integer id) {
        return em.find(SystemSettings.class, id);
    }

    public SystemSettings save(SystemSettings settings) {
        em.persist(settings);
        return settings;
    }

    public SystemSettings update(SystemSettings settings) {
        return em.merge(settings);
    }

    public void delete(Integer id) {
        em.remove(em.find(SystemSettings.class, id));
    }

    public SystemSettings getByOrganization(Organization organization) {
        Query query = em.createQuery("SELECT e FROM SystemSettings e  WHERE e.organization=:organization");
        query.setParameter("organization", organization);
        return (SystemSettings) query.getSingleResult();
    }

}
